/**
 * Created by k.danovsky on 13.05.2016.
 */

(function () {
  'use strict';

  angular.module('BlurAdmin.theme')
    .config(config);

  /** @ngInject */
  function config(baConfigProvider, colorHelper, $provide) {
    //baConfigProvider.changeTheme({blur: true});
    //
    //baConfigProvider.changeColors({
    //  default: 'rgba(#000000, 0.2)',
    //  defaultText: '#ffffff',
    //  dashboard: {
    //    white: '#ffffff',
    //  },
    //});

    $provide.decorator('taOptions', ['taRegisterTool', 'taToolFunctions', '$delegate', 'qiniuToken', '$qupload', '$uibModal', function (taRegisterTool, taToolFunctions, taOptions, qiniuToken, $qupload, $uibModal) {
      taRegisterTool('uploadImage', {
        iconclass: "fa fa-file-image-o",
        action: function (deferred, restoreSelection) {
          var textAngular = this;
          var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'app/pages/support/uploadImgModal.html',
            controller: 'UploadImgModalCtrl',
          });

          modalInstance.result.then(function (img) {
            qiniuToken.imgBaseUrl().then(function (data) {
              var imgUrl = data + img;
              console.log(imgUrl);
              restoreSelection();
              textAngular.$editor().wrapSelection('insertImage', imgUrl);
              deferred.resolve();
            })
          });
          return false;
        }
      });


    // taRegisterTool('insertImageFile', {
    //   display: '<div style="height: 38px">' +
    //               '<label for="fileUpload" >' +
    //                 '<i class="fa fa-cloud-upload"></i>' +
    //               '</label>' +
    //               '<input type="file" id="fileUpload" name="upload" style="display: none;" />' +
    //            '</div>',
    //   action: function(deferred, restoreSelection){
    //     var editor = this.$editor();
    //     var fileInput=angular.element(document.querySelector('#fileUpload'));
    //     fileInput.bind('change', function(e) {
    //       e.stopImmediatePropagation();
    //       var file = fileInput[0].files[0];
    //       var imageType = /image.*/;
    //       if (file.type.match(imageType)) {
    //         var imgUrl;
    //         var imgBaseUrl = qiniuToken.imgBaseUrl();
    //         qiniuToken.imgToken().then(function (data) {
    //           var token = data.data.token;
    //           $qupload.upload({
    //             key: '', // hm/msjt
    //             file: file,
    //             token: token
    //           }).then(function (response) {
    //             imgUrl = imgBaseUrl + response.key;
    //             console.log(imgUrl);
    //             restoreSelection();
    //             editor.wrapSelection('insertImage', imgUrl);
    //           });
    //         });
    //       } else {
    //         editor.wrapSelection('innerText', 'File not supported!');
    //       }
    //     });
    //
    //     return false;
    //   }
    // });

    taOptions.toolbar[1].push('uploadImage');
    // taOptions.toolbar[1].push('insertImageFile');
    return taOptions;
  }]);

    $provide.decorator('taOptions', ['taRegisterTool', 'taToolFunctions', '$delegate', '$uibModal', function(taRegisterTool, taToolFunctions, taOptions, $uibModal){
      // $delegate is the taOptions we are decorating
      // register the tool with textAngular
      taRegisterTool('insertTable', {
        iconclass: "fa fa-table",
        tooltiptext: "Insert Table",
        action: function(deferred) {
          var textAngular = this
          var savedSelection = rangy.saveSelection()
          $uibModal.open({
            // size: 'sm',
            controller: 'InsertTableModalInstanceController',
            templateUrl: 'app/pages/support/insertTableModal.html'
          }).result.then(function (result) {
              rangy.restoreSelection(savedSelection)
              textAngular.$editor().wrapSelection('insertHTML', result)
              deferred.resolve()
            },
            function () {
              rangy.restoreSelection(savedSelection)
              deferred.resolve()
            }
          );
          return false;
        }/*,
         onElementSelect: {
         element: 'table',
         action: tableOnSelectAction
         }*/
      });
      // add the button to the default toolbar definition
      taOptions.toolbar[1].push('insertTable');
      return taOptions;
    }]);


  }
})();
